import { _decorator, BoxCollider2D, Component, EPhysics2DDrawFlags, ERigidBody2DType, Node, PhysicsSystem2D, RigidBody2D, TiledMap, v2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('initTiledMap')
export class initTiledMap extends Component {
    start() {
        // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.All;
        this.init();
    }
    init(){
        let tiledMap = this.node.getComponent(TiledMap);
        // console.log(tiledMap);
        let tiledSize = tiledMap.getTileSize();
        // console.log(tiledSize);
        let layer = tiledMap.getLayer("ground");
        // console.log(layer);
        let layerSize = layer.getLayerSize();
        for (let i = 0; i < layerSize.width; i++) {
            for (let j = 0; j < layerSize.height; j++) {
                let tiled = layer.getTiledTileAt(i, j, true);
                if (tiled.grid !== 0) {
                    let body = tiled.node.addComponent(RigidBody2D);
                    body.type = ERigidBody2DType.Static;
                    body.wakeUp();
                    console.log(body.group);
                    let collider = tiled.node.addComponent(BoxCollider2D);
                    collider.group = 1<<2;
                    collider.offset = v2(tiledSize.width / 2, tiledSize.height / 2);
                    collider.size = tiledSize;
                    collider.apply();
                }
            }
        }
    }

    update(deltaTime: number) {

    }
}


